/////////////////////////////////////////////////////////////////////////////// 
//Filename: CycleTimer.java
//
//Authors: Noah Wonhala & Scott Cavanaugh
//
//Description:  Class for keeping track of cooldowns and cycles.  This class
//implements the Thread interface whose work is to start a timer and check its 
//countdown progress until the completion of a preset duration, at which point
//it will flag the isWorking member as false to signal completion to the parent
//class or caller.
//
////////////////////////////////////////////////////////////////////////////////

package com.sngamelib.topdownshooter;

import java.util.Date;

public class CycleTimer extends Thread
{
	////////////////////////////////////////////////////////////////////
	//Member Variables
	////////////////////////////////////////////////////////////////////
	
	private int duration;
	private int counter;
	private Date startTime;
	private boolean isWorking;
	
	////////////////////////////////////////////////////////////////////
	//Constructors
	////////////////////////////////////////////////////////////////////
	
	CycleTimer()
	{
		duration = 0;
		counter = 0;
		startTime = new Date();
	}
	
	CycleTimer(int _d, int _c)
	{
		duration = _d;
		counter = _c;
		startTime = new Date();
	}
	
	CycleTimer(int _d, int _c, Date _st)
	{
		duration = _d;
		counter = _c;
		startTime = _st;
	}
	
	/////////////////////////////////////////////////////////////////////////////
	// Accessor Methods
	/////////////////////////////////////////////////////////////////////////////
	
	public int getDuration()
	{
		return duration;
	}
	
	public int getCounter()
	{
		return counter;
	}
	
	public Date getStartTime()
	{
		return startTime;
	}
	
	public void setDuration(int _d)
	{
		duration = _d;
	}
	
	public void setCounter(int _c)
	{
		counter = _c;
	}
	
	public void setStartTime(Date _st)
	{
		startTime = new Date(_st.getTime());
	}
	
	public boolean isWorking()
	{
		return isWorking;
	}
	
	/////////////////////////////////////////////////////////////////////////////
	// Worker Methods
	/////////////////////////////////////////////////////////////////////////////
	
	public Date getTimeRemainingDate()
	{
		//subtract the start time as a long from the current time as a long.  Subtract the result from the duration converted to ms
		//and return the value as a Date object.
		return new Date((duration*1000) - (new Date().getTime() - startTime.getTime()));
	}
	
	public long getTimeRemainingLong()
	{
		//subtract the start time as a long from the current time as a long. Subtract the result from the duration converted to ms
		//and return the value as a long.
		return ((duration*1000) - new Date().getTime() - startTime.getTime());
	}
	
	public int getTimeRemainingInt()
	{
		//subtract the start time as a long from the current time as a long then divide the result by 1000 (for milliseconds).
		//Subtract the result from duration and return the value as an integer.
		return (int)(duration - (new Date().getTime() - startTime.getTime()) / 1000);
	}
	
	
	/////////////////////////////////////////////////////////////////////////////
	// Thread Methods
	/////////////////////////////////////////////////////////////////////////////
	
	//Thread worker function
	public void run()
	{
		isWorking = true; //this cycle is now running
		startTime = new Date(new Date().getTime()); // get the start time.
		
		while(getTimeRemainingInt() > 0)
		{
			counter = getTimeRemainingInt(); //possibly to be removed, keeps track of cooldown counter, probably not needed we can just call said function.
			try
			{
				this.wait(1000); //change this to increase timer sensitivity for ui updates;
			}
			catch (InterruptedException ie)
			{
				//do something, this is necessary for java not to act like a dick.
				//... come at me bro.
			}
		}
		
		isWorking = false; //this cycle is over;
		
		try
		{
			this.join(); //rejoin main thread
		}
		catch(InterruptedException ie)
		{
			//see above for try/catch bullshit. -_-
		}
	}
}
